//
//  BVGoodsViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/11/1.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVGoodsViewController.h"
#import "BVGoodsMenuCell.h"
#import "UIButton+BVButtonAnimation.h"
#import "BVGoodsDetailViewController.h"
#import "BVTabBarController.h"
#import "UIView+Extension.h"
#import "BVCategoryTool.h"
#import "MBProgressHUD.h"
#import "BVLoginViewController.h"

#define GOODSLIST_CELL_WIDTH      (ScreenW-6-12)
#define GOODSLIST_CELL_IDENTIFIER @"BVGoodsMenuCell"

@interface BVGoodsViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *shopping_cart;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *shoppingcart_badge;
@property (nonatomic, assign) NSInteger badgeValue;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *filterArr;
@property (nonatomic, strong) BVCategoryTool *categoryTool;

@property (weak, nonatomic) IBOutlet UIView *filterView;
@property (strong, nonatomic) IBOutlet UIView *filterV;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, assign) NSInteger status;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, strong) NSMutableArray *goods_list;
@property (nonatomic, strong) BVGoodsList *goodsList;

@end

@implementation BVGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:self.name];
    
}

- (void)setFilterView:(UIView *)filterView {
    _filterView = filterView;
    [filterView addSubview:self.filterV];
}

- (NSInteger)status {
    if (!_status) {
        _status = 1;
    }
    return _status;
}
- (void)setFilterV:(UIView *)filterV {
    _filterV = filterV;
    [filterV setFrame:CGRectMake(0, 0, ScreenW, 40)];
}

- (void)setFilterArr:(NSArray *)filterArr {
    _filterArr = filterArr;
    [self filterClick:filterArr.firstObject];
    for (UIButton *filterBtn in filterArr) {
        if (filterBtn.tag != 1) {
            [filterBtn setImage:IMAGE_NAMED(@"arrow_normal") forState:UIControlStateNormal];
            [filterBtn setImage:IMAGE_NAMED(@"arrow_selected") forState:UIControlStateSelected];
        }
        if (filterBtn.tag == 4) {
            [filterBtn setImage:IMAGE_NAMED(@"arrow_up") forState:UIControlStateNormal];
        }
        [filterBtn setTitleColor:[UIColor BVBaseTextBlackColor] forState:UIControlStateNormal];
        [filterBtn setTitleColor:[UIColor BVBaseOrangeColor] forState:UIControlStateSelected];
        [filterBtn addTarget:self action:@selector(filterClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark -
#pragma mark - 刷新数据
- (void)filterClick:(UIButton *)button {
    [self settingChildView:button];
    
    if (button.tag == 1) {
        self.type = @"0";
    } else if (button.tag == 2) {
        self.type = @"1";
    } else if (button.tag == 3) {
        self.type = @"2";
    } else if (button.tag == 4) {
        self.type = self.status == 1 ? @"3":@"4";
    }
    
    [self.hud show:YES];
    [self.categoryTool fetchGoodsListByChildID:self.childId type:self.type cityID:self.user.city_id success:^(BVGoodsList *goodsList) {
        [self.hud hide:YES];
        self.goodsList = goodsList;
        self.goods_list = [NSMutableArray arrayWithArray:goodsList.goods_list];
        [self.collectionView reloadData];
        [self updataUI];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"加载失败"];
    }];
}

- (void)updataUI {
    [self.shoppingcart_badge setTitle:[NSString stringWithFormat:@"%@",self.goodsList.goods_num] forState:UIControlStateNormal];
}

- (void)setCollectionView:(UICollectionView *)collectionView {
    _collectionView = collectionView;
    [collectionView registerNib:NIB_NAMED(GOODSLIST_CELL_IDENTIFIER) forCellWithReuseIdentifier:GOODSLIST_CELL_IDENTIFIER];
}

#pragma mark -
#pragma mark - 添加购物车
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.goods_list.count == 0) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = IMAGE_NAMED(@"nullData");
        imageView.contentMode = UIViewContentModeCenter;
        collectionView.backgroundView = imageView;
        return 0;
    } else {
        collectionView.backgroundView = nil;
        return 1;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goods_list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BVGoodsMenuCell *goodsmenu_cell = [collectionView dequeueReusableCellWithReuseIdentifier:GOODSLIST_CELL_IDENTIFIER forIndexPath:indexPath];
    BVGoods_list *goods = self.goods_list[indexPath.row];
    goodsmenu_cell.goods_list = goods;
    
    goodsmenu_cell.shoppingCartBlock = ^(UIButton *button,NSString *ID){
//        NSLog(@"ID==>>%@",ID);
        [self addShoppingCartClick:button ID:ID];
    };
    return goodsmenu_cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(GOODSLIST_CELL_WIDTH/2, 220);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(6, 6, 6, 6);
}

#pragma mark -
#pragma mark - 添加购物车
- (void)addShoppingCartClick:(UIButton *)button ID:(NSString *)ID {
    if (self.user.login) {
        __weak BVGoodsViewController *weakSelf = self;
        [self.categoryTool addGoodsToShoppingCart:ID success:^{
            NSInteger badgeValue = [weakSelf.shoppingcart_badge.titleLabel.text integerValue];
            badgeValue++;
            [UIButton buttonClickAnimation:weakSelf.shoppingcart_badge];
            [weakSelf.shoppingcart_badge setTitle:[NSString stringWithFormat:@"%ld",badgeValue] forState:UIControlStateNormal];
            [weakSelf showMessage:@"添加购物车成功"];
        } failure:^(NSError *error) {
        }];
    } else {
        BVLoginViewController *login = [[BVLoginViewController alloc] init];
        [self.navigationController pushViewController:login animated:YES];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BVGoodsDetailViewController *goods_detail = [[BVGoodsDetailViewController alloc] init];
    [self.navigationController pushViewController:goods_detail animated:YES];
}

- (IBAction)shoppingCartClick:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:NO];
    BVTabBarController *tabBarVc = (BVTabBarController *)BVKeyWindow.rootViewController;
    tabBarVc.selectedIndex = 2;
}

- (void)settingChildView:(UIButton *)button {
    if (button != self.selectedBtn) {
        self.selectedBtn.selected = NO;
        button.selected = YES;
        self.selectedBtn = button;
    }else{
        self.selectedBtn.selected = YES;
    }
    
    if (button.tag == 4) {
        if (self.status == 1) {
            self.status = 2;
            [button setImage:IMAGE_NAMED(@"arrow_up_selected") forState:UIControlStateSelected];
        } else if (self.status == 2) {
            self.status = 1;
            [button setImage:IMAGE_NAMED(@"arrow_up_normal") forState:UIControlStateSelected];
        }
    }
    
    float filterW = ScreenW/4;
    float btnW = button.frame.size.width;
    float spaceW  = (filterW - btnW)/2;
    [UIView animateWithDuration:0.2f animations:^{
        self.lineView.x = (button.tag - 1)*btnW + (2*button.tag - 1)*spaceW + 5;
    }];
}

- (BVCategoryTool *)categoryTool {
    if (!_categoryTool) {
        _categoryTool = [[BVCategoryTool alloc] init];
    }
    return _categoryTool;
}

- (NSString *)type {
    if (!_type) {
        _type = @"0";
    }
    return _type;
}

@end
